package html;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import config.Config;

import reservation.Reservation;

/**
 * Cette classe permet de générer un emploi du temps en Html en fonction de la
 * semaine voulu
 * 
 * @author FAUCONNIER Simon LUTTIAU Hadrien Bennour Salah
 * 
 */

public class Html {

	private ArrayList<Jour> semaine = new ArrayList<Jour>();
	private String pathHtml = Config.PATH_HTML;

	public void initSemaine() {
		for (int i = 0; i < 6; i++) {
			semaine.add(new Jour());
		}

	}

	public void generateHtml(int numSemaine, String nom, boolean prof) {
		initSemaine();
		List<Reservation> listeReservation = Reservation.getListeReservation();

		for (Reservation res : listeReservation) {

			if (prof) {
				if (res.getProfesseur().getNom().equals(nom)) {
					Date date = res.getSeance().getDate();
					if (sameWeek(numSemaine, date)) {
						int i = res.getSeance().getDate().getDay() - 1;

						Jour jour = semaine.get(i);
						jour.setCreneauI(res.getSeance().getCreneau(),
								new Creneau(res.getProfesseur().getNom(), res
										.getSalle().getId(), res.getSeance()
										.getNomSeance()));
						semaine.set(i, jour);

					} else {
						if (res.getSeance().getGroupe().getNomGroupe().equals(
								"nom")) {
							Date date1 = res.getSeance().getDate();
							if (sameWeek(numSemaine, date1)) {
								int i = res.getSeance().getDate().getDay() - 1;

								Jour jour = semaine.get(i);
								jour.setCreneauI(res.getSeance().getCreneau(),
										new Creneau(res.getProfesseur()
												.getNom(), res.getSalle()
												.getId(), res.getSeance()
												.getNomSeance()));
								semaine.set(i, jour);
							}

						}
					}
				}
			}
		}

		PrintWriter ecrire;
		try {
			ecrire = new PrintWriter(new BufferedWriter(
					new FileWriter(pathHtml)));

			ecrire
					.println("<!DOCTYPE html>" + "<html>" + "  <head>"
							+ "    <title>Semaine n°"
							+ numSemaine
							+ "</title>"
							+ "    <meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\">"
							+ "  </head>"
							+ "  <body>"
							+ "    <p></p>"
							+ "    <table style=\"width: 1453px; height: 483px;\" border=\"1\">"
							+ "      <tbody>"
							+ "        <tr>"
							+ "          <td style=\"text-align: center;\"><br>"
							+ "          </td>"
							+ "          <td style=\"text-align: center;\">Lundi</td>"
							+ "          <td style=\"text-align: center;\">Mardi</td>"
							+ "          <td style=\"text-align: center; margin-left: -40px;\">Mercredi</td>"
							+ "          <td style=\"text-align: center; margin-left: -59px;\">Jeudi</td>"
							+ "          <td style=\"text-align: center; margin-left: -60px;\">Vendredi</td>"
							+ "          <td style=\"text-align: center;\">Samedi</td>"
							+ "        </tr>"
							+ "        <tr>"
							+ "          <td style=\"width: 74px;\">8h<br>"
							+ "            <br>"
							+ "            <br>"
							+ "            <br>"
							+ "            <br>"
							+ "            10h</td>"
							+ "          <td style=\"height: 125px; width: 232.567px; text-align: center;\">"
							+ semaine.get(0).getCreneau(0).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(0).getCreneau(0).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(0).getCreneau(0).getMatiere()
							+ "</td>"
							+ "          <td style=\"width: 218.517px; text-align: center;\""
							+ semaine.get(1).getCreneau(0).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(1).getCreneau(0).getNumSalle()
							+ "<br>"
							+ "           "
							+ semaine.get(1).getCreneau(0).getMatiere()
							+ "</td>"
							+ "          <td style=\"margin-left: 67px; width: 220.783px; text-align: center;\">"
							+ semaine.get(2).getCreneau(0).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(2).getCreneau(0).getNumSalle()
							+ "<br>"
							+ "           "
							+ semaine.get(2).getCreneau(0).getMatiere()
							+ "</td>"
							+ "          <td style=\"width: 220.633px; text-align: center;\">"
							+ semaine.get(3).getCreneau(0).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(3).getCreneau(0).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(3).getCreneau(0).getMatiere()
							+ "</td>"
							+ "          <td style=\"width: 220.25px; text-align: center;\">"
							+ semaine.get(4).getCreneau(0).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(4).getCreneau(0).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(4).getCreneau(0).getMatiere()
							+ "</td>"
							+ "          <td style=\"width: 220.333px; text-align: center;\">"
							+ semaine.get(5).getCreneau(0).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(5).getCreneau(0).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(5).getCreneau(0).getMatiere()
							+ "</td>"
							+ "        </tr>"
							+ "        <tr>"
							+ "          <td style=\"height: 122px;\">10h15<br>"
							+ "            <br>"
							+ "            <br>"
							+ "            <br>"
							+ "            <br>"
							+ "            12h15</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(0).getCreneau(1).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(0).getCreneau(1).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(0).getCreneau(1).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(1).getCreneau(1).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(1).getCreneau(1).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(1).getCreneau(1).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(2).getCreneau(1).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(2).getCreneau(1).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(2).getCreneau(1).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(3).getCreneau(1).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(3).getCreneau(1).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(3).getCreneau(1).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(4).getCreneau(1).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(4).getCreneau(1).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(4).getCreneau(1).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(5).getCreneau(1).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(5).getCreneau(1).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(5).getCreneau(1).getMatiere()
							+ "</td>"
							+ "        </tr>"
							+ "        <tr>"
							+ "          <td style=\"height: 25px;\"><br>"
							+ "          </td>"
							+ "          <td style=\"height: 25px; text-align: center;\"><br>"
							+ "          </td>"
							+ "          <td style=\"height: 25px; text-align: center;\"><br>"
							+ "          </td>"
							+ "          <td style=\"height: 25px; text-align: center;\"><br>"
							+ "          </td>"
							+ "          <td style=\"height: 25px; text-align: center;\"><br>"
							+ "          </td>"
							+ "          <td style=\"height: 25px; text-align: center;\"><br>"
							+ "          </td>"
							+ "          <td style=\"height: 25px; text-align: center;\"><br>"
							+ "          </td>"
							+ "        </tr>"
							+ "        <tr>"
							+ "        <td style=\"height: 122px;\">13h30<br>"
							+ "            <br>"
							+ "            <br>"
							+ "            <br>"
							+ "            <br>"
							+ "            15h30</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(0).getCreneau(2).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(0).getCreneau(2).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(0).getCreneau(2).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(1).getCreneau(2).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(1).getCreneau(2).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(1).getCreneau(2).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(2).getCreneau(2).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(2).getCreneau(2).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(2).getCreneau(2).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(3).getCreneau(2).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(3).getCreneau(2).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(3).getCreneau(2).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(4).getCreneau(2).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(4).getCreneau(2).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(4).getCreneau(2).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 122px; text-align: center;\">"
							+ semaine.get(5).getCreneau(2).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(5).getCreneau(2).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(5).getCreneau(2).getMatiere()
							+ "</td>"
							+ "        </tr>"
							+ "        <tr>"
							+ "          <td style=\"height: 124px;\">15h45<br>"
							+ "            <br>"
							+ "            <br>"
							+ "            <br>"
							+ "            <br>"
							+ "            17h45</td>"
							+ "          <td style=\"height: 124px; margin-left: 14px; text-align: center;\">"
							+ semaine.get(0).getCreneau(3).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(0).getCreneau(3).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(0).getCreneau(3).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 124px; text-align: center;\"><br>"
							+ semaine.get(1).getCreneau(3).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(1).getCreneau(3).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(1).getCreneau(3).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 124px; text-align: center;\">"
							+ semaine.get(2).getCreneau(3).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(2).getCreneau(3).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(2).getCreneau(3).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 124px; text-align: center;\">"
							+ semaine.get(3).getCreneau(3).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(3).getCreneau(3).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(3).getCreneau(3).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 124px; text-align: center;\">"
							+ semaine.get(4).getCreneau(3).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(4).getCreneau(3).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(4).getCreneau(3).getMatiere()
							+ "</td>"
							+ "          <td style=\"height: 124px; text-align: center;\">"
							+ semaine.get(5).getCreneau(3).getNomProf()
							+ "<br>"
							+ "            "
							+ semaine.get(5).getCreneau(3).getNumSalle()
							+ "<br>"
							+ "            "
							+ semaine.get(5).getCreneau(3).getMatiere()
							+ "</td>"
							+ "        </tr>"
							+ "      </tbody>"
							+ "    </table>"
							+ "    <p><br>"
							+ "    </p>"
							+ "  </body>" + "</html>");

			ecrire.close();
		} catch (IOException e) {
			System.out.println("probleme html");
		}

		try {
			Runtime.getRuntime().exec("firefox " + pathHtml);
		} catch (IOException e) {
			System.out.println("erreur firefox");
		}

	}

	public boolean sameWeek(int numSemaine, Date date) {

		Calendar calendar = new GregorianCalendar();

		calendar.set(Calendar.WEEK_OF_YEAR, numSemaine);
		calendar.set(Calendar.YEAR, 2013);

		if (calendar.get(Calendar.MONTH) == date.getMonth()) {
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			if (calendar.get(Calendar.DATE) <= date.getDate()
					&& (calendar.get(Calendar.DATE) + 6) >= date.getDate()) {
				return true;
			}
		}

		return false;
	}
}
